package command

import (
	"gitee.com/csingo/cContext"
	"gitee.com/csingo/cDB"
	"gitee.com/csingo/cLog"
	"satis-go/resource"
	"strings"
)

type InitSQLCommand struct{}

func (i *InitSQLCommand) CommandName() (app, name string) {
	return "collector", "InitSQLCommand"
}

func (i *InitSQLCommand) CreateTable() {
	ctx := cContext.New()
	execSql, err := resource.Asset("resource/sql/satis.sql")
	if err != nil {
		cLog.WithContext(ctx, map[string]interface{}{
			"err": err.Error(),
		}).Panic("获取初始化SQL错误")
	}
	execsqls := strings.Split(string(execSql), ";")

	for i, execsql := range execsqls {
		if i == len(execsqls)-1 {
			continue
		}
		err = cDB.Connection("default").Exec(execsql).Error
		if err != nil {
			cLog.WithContext(ctx, map[string]interface{}{
				"sql": execSql,
				"err": err.Error(),
			}).Panic("执行初始化SQL错误")
		}
	}
}
